<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: SHA1 哈希</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'url-parsing';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'base64-encoding';
          }
          
      }
  </script>
  <body>

    <div class="example" id="sha1-hashes">
      <h2><a href="./">Go by Example 中文版</a>: SHA1 哈希</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p><a href="http://en.wikipedia.org/wiki/SHA-1"><em>SHA1 散列（hash）</em></a>
经常用于生成二进制文件或者文本块的短标识。
例如，<a href="http://git-scm.com/">git 版本控制系统</a> 大量的使用了 SHA1
来标识受版本控制的文件和目录。
这是 Go 中如何进行 SHA1 散列计算的例子。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/ye-E54B_QnG"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>Go 在多个 <code>crypto/*</code> 包中实现了一系列散列函数。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;crypto/sha1&quot;</span>
    <span class="s">&quot;fmt&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">s</span> <span class="o">:=</span> <span class="s">&quot;sha1 this string&quot;</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>产生一个散列值的方式是 <code>sha1.New()</code>，<code>sha1.Write(bytes)</code>，
然后 <code>sha1.Sum([]byte{})</code>。这里我们从一个新的散列开始。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">h</span> <span class="o">:=</span> <span class="nx">sha1</span><span class="p">.</span><span class="nx">New</span><span class="p">()</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>写入要处理的字节。如果是一个字符串，
需要使用 <code>[]byte(s)</code> 将其强制转换成字节数组。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">h</span><span class="p">.</span><span class="nx">Write</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">s</span><span class="p">))</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p><code>Sum</code> 得到最终的散列值的字符切片。<code>Sum</code> 接收一个参数，
可以用来给现有的字符切片追加额外的字节切片：但是一般都不需要这样做。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">bs</span> <span class="o">:=</span> <span class="nx">h</span><span class="p">.</span><span class="nx">Sum</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>SHA1 值经常以 16 进制输出，例如在 git commit 中。
我们这里也使用 <code>%x</code> 来将散列结果格式化为 16 进制字符串。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre>    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;%x\n&quot;</span><span class="p">,</span> <span class="nx">bs</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>运行程序计算散列值，并以可读的 16 进制格式输出。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go run sha1-hashes.go
<span class="go">sha1 this string</span>
<span class="go">cf23df2207d99a74fbe169e3eba035e633b65d94</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>你可以使用和上面相似的方式来计算其他形式的散列值。
例如，计算 MD5 散列，引入 <code>crypto/md5</code> 并使用 <code>md5.New()</code> 方法。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>注意，如果你需要密码学上的安全散列，你需要仔细的研究一下
<a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">加密散列函数</a>。</p>

          </td>
          <td class="code empty">
            
          
          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="base64-encoding">Base64 编码</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"crypto/sha1\"\u000A    \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A    s := \"sha1 this string\"\u000A');codeLines.push('    h := sha1.New()\u000A');codeLines.push('    h.Write([]byte(s))\u000A');codeLines.push('    bs := h.Sum(nil)\u000A');codeLines.push('    fmt.Println(s)\u000A    fmt.Printf(\"%x\\n\", bs)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
